//
//  UIButton+SYAdd.m
//  SYSDK
//
//  Created by ligq on 2020/4/13.
//  Copyright © 2020 synjones. All rights reserved.
//

#import "UIButton+SYAdd.h"

@implementation UIButton (SYAdd)

+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font{
    
    return [self buttonWithTitle:title titleColor:titleColor font:font backgroundColor:nil];
}

+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backgroundColor:(UIColor *)backgroundColor{
    
    UIButton *button = [[self alloc] init];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font      = font;
    [button setBackgroundColor:backgroundColor];
    
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title titleSel:(NSString *)titleSel titleColor:(UIColor *)titleColor font:(UIFont *)font {
    
    return [self buttonWithTitle:title titleSel:titleSel titleColor:titleColor titleSelColor:nil font:font];
}

+ (instancetype)buttonWithTitle:(NSString *)title titleSel:(NSString *)titleSel titleColor:(UIColor *)titleColor titleSelColor:(UIColor *)titleSelColor font:(UIFont *)font {
    
    UIButton *button = [[self alloc] init];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:titleSel forState:UIControlStateSelected];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleSelColor forState:UIControlStateSelected];
    button.titleLabel.font      = font;
    
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font cornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor backgroundColor:(UIColor *)backgroundColor{
    
    UIButton *button = [[self alloc] init];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setBackgroundColor:backgroundColor];
    button.titleLabel.font      = font;
    button.layer.cornerRadius   = radius;
    button.layer.masksToBounds  = YES;
    button.layer.borderWidth    = borderWidth;
    button.layer.borderColor    = borderColor.CGColor;
    
    return button;
}

+ (instancetype)buttonWithTitle:(NSString *)title titleSel:(NSString *)titleSel titleColor:(UIColor *)titleColor titleSelColor:(UIColor *)titleSelColor font:(UIFont *)font normalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage {
    
    UIButton *button = [[self alloc] init];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:titleSel forState:UIControlStateSelected];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleSelColor forState:UIControlStateSelected];
    button.titleLabel.font      = font;
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    
    [button setEdgeInsets];
    
    return button;
}

+ (instancetype)buttonWithTarget:(id)target action:(SEL)action event:(UIControlEvents)event {
    
    UIButton *button = [[self alloc] init];
    [button addTarget:target action:action forControlEvents:event];
    
    return button;
}

#pragma mark -  成员方法

- (void)setupButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font {
    
    [self setupButtonWithTitle:title titleSel:nil titleColor:titleColor titleSelColor:nil font:font];
}

- (void)setupButtonWithTitle:(NSString *)title titleSel:(NSString *)titleSel titleColor:(UIColor *)titleColor titleSelColor:(UIColor *)titleSelColor font:(UIFont *)font {
    
    [self setTitle:title                forState:UIControlStateNormal];
    [self setTitle:titleSel             forState:UIControlStateSelected];
    [self setTitleColor:titleColor      forState:UIControlStateNormal];
    [self setTitleColor:titleSelColor   forState:UIControlStateSelected];
    self.titleLabel.font      = font;
}

- (void)changeStyleWithTitleColor:(UIColor *)titleColor font:(UIFont *)font{
    
    [self setTitleColor:titleColor forState:UIControlStateNormal];
    self.titleLabel.font      = font;
}

- (void)changeStyleWithTitleColor:(UIColor *)titleColor backgroundColor:(UIColor *)backgroundColor {
    
    [self setTitleColor:titleColor forState:UIControlStateNormal];
    self.backgroundColor = backgroundColor;
}

- (void)changeStyleWithTitleColor:(UIColor *)titleColor backgroundColor:(UIColor *)backgroundColor borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    
    [self setTitleColor:titleColor forState:UIControlStateNormal];
    [self setBackgroundColor:backgroundColor];
    self.layer.borderWidth  = borderWidth;
    self.layer.borderColor  = borderColor.CGColor;
}

- (void)changeTitle:(NSString *)title{
    
    [self setTitle:@"" forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)changeTitle:(NSString *)title titleColor:(UIColor *)titleColor {
    
    [self setTitle:@"" forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)changeStyleWithTitle:(NSString *)title titleColor:(UIColor *)titleColor backgroundColor:(UIColor *)backgroundColor{
    
    [self changeTitle:title];
    [self changeStyleWithTitleColor:titleColor backgroundColor:backgroundColor];
}

- (void)changeStyleWithTitleColor:(UIColor *)titleColor borderColor:(UIColor *)borderColor {

    [self setTitleColor:titleColor forState:UIControlStateNormal];
    self.layer.borderColor  = borderColor.CGColor;
}

- (instancetype)setupButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage{
    
    [self setImage:normalImage forState:UIControlStateNormal];
    [self setImage:selectedImage forState:UIControlStateSelected];
    
    return self;
}

- (instancetype)setupButtonBackgroundWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage {
    [self setBackgroundImage:normalImage forState:UIControlStateNormal];
    [self setBackgroundImage:selectedImage forState:UIControlStateSelected];
    
    return self;
}

- (void)setEdgeInsets {
    self.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
}

- (void)setEdgeInsetsHorizontal:(CGFloat)offset {
    self.imageEdgeInsets = UIEdgeInsetsMake(0, -offset, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, offset, 0, 0);
}

@end
